#include "mysql.h"
#include "config.h"

int mysql_connect(MYSQL **connect) {
    config_t configs[50];
    int n;
    read_conf("../conf/mysql.conf", configs, &n);
    
    char host[CONFIG_LEN] = {0};
    char username[CONFIG_LEN] = {0};
    char password[CONFIG_LEN] = {0};
    char database[CONFIG_LEN] = {0};

    get_conf_value(configs, n, "host", host);
    get_conf_value(configs, n, "username", username);
    get_conf_value(configs, n, "password", password);
    get_conf_value(configs, n, "database", database);

    printf("host = %s, username = %s, password = %s, database = %s\n", host, username, password, database);


    *connect = mysql_init(NULL);
    if (!mysql_real_connect(*connect, host, username, password, database, 0, NULL, 0)) {
        fprintf(stderr, "Fail to connect database: %s\n", mysql_error(*connect));
        return -1;
    }

    free_configs(configs, n);
    return 0;
}
